package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.SourceData;
import com.jzg.jcptadmin.viewinterface.SourceInterface;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2016/1/6.
 */
public class SourcePresenter extends BasePresenter<SourceInterface> {

    private DataManager dataManager;
    private SourceInterface mView;
    private Subscription mSubscription;

    public SourcePresenter(DataManager dataManager) {
        super();
        this.dataManager = dataManager;
    }

    @Override
    public void attachView(SourceInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;

    }
    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    /**
     * 加载车源机构
     */
    public void LoadSourceData(String str,String userId){
        checkViewAttached();
        mSubscription = dataManager.loadSourceData(str,userId).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Observer<SourceData>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if(e!=null&& mView!=null){

                    mView.showError(e.toString());
                }
            }

            @Override
            public void onNext(SourceData sourceData) {
                if(Constant.SUCCESS ==sourceData.getStatus()){
                    mView.showDatas(sourceData);

                }

            }
        });
    }


}
